import { Button, VerticalBox, LineEdit, GroupBox, ComboBox, CheckBox } from "std-widgets.slint";

export component AppWindow inherits Window {
    title: "spmv benchmark";
    preferred-width: 800px;
    preferred-height: 600px;

    callback from-file();
    callback from-rand();
    callback edit-file-path(string);
    callback edit-rows(string);
    callback edit-nnz(string);
    callback edit-numberxy(string);
    callback init_matrix();
    callback select-matrix-format(string);
    callback cpu_calc();
    callback gpu_calc();
    callback start-calc();

    property <int> g_width: 800;
    property <int> g_height: 600;
    in-out property <int> number_xy: 10;
    in-out property <string> file_path: "/home/xjb/Desktop/code/Benchmark_SpMV_using_CSR5/CSR5_avx2/Freescale1.mtx";
    in-out property <bool> is_from_file <=> from_file.checked;
    in-out property <bool> is_cpu_gpu <=> from_cpu.checked;
    in-out property <int> rows:0;
    in-out property <int> nnz:0;

    VerticalLayout {
        x: 0px;
        y: 0px;
        width: g_width * 0.3px;
        height: parent.height;
        LineEdit {
            placeholder-text: "please input file path";
            horizontal-alignment: center;
            //border-width: 5px;
            height: 48px;
            width: parent.width;
            text: file_path;
            accepted => {
                root.edit-file-path(self.text);
            }
            // edited(file_path) => {
            //     root.edit-file-path(file_path);
            // }
        }

        HorizontalLayout {
            from_file := CheckBox {
                text: "从文件读取";
                width: g_width * 0.25px * 0.5;
                checked: true;
                toggled => {
                    root.from-file();
                    from_rand.checked = !self.checked;
                }
            }

            from_rand := CheckBox {
                text: "随机生成";
                width: g_width * 0.25px * 0.5;
                checked: false;
                toggled => {
                    root.from-rand();
                    from_file.checked = !self.checked;
                }
            }
        }

        VerticalLayout {
            HorizontalLayout {
                Text {
                    text: "ROWS";
                    width: 40px;
                }

                LineEdit {
                    text: rows;
                    horizontal-alignment: center;
                    placeholder-text: "稀疏矩阵行数";
                    height: 24px;
                    accepted => {
                        root.edit-rows(self.text);
                    }
                }
            }

            HorizontalLayout {
                Text {
                    text: "NNZ";
                    width: 40px;
                }

                LineEdit {
                    text: nnz;
                    horizontal-alignment: center;
                    placeholder-text: "非零元素总数";
                    height: 24px;
                    accepted => {
                        root.edit-nnz(self.text);
                    }
                }
            }

            HorizontalLayout {
                Text {
                    text: "iter";
                    width: 40px;
                }

                LineEdit {
                    text: number_xy;
                    horizontal-alignment: center;
                    placeholder-text: "计算次数";
                    height: 24px;
                    accepted => {
                        root.edit-numberxy(self.text);
                    }
                }
            }

            Button {
                text: "初始化";
                height: 30px;
                clicked => {
                    root.init_matrix();
                }
            }
        }

        HorizontalLayout {
            from_cpu := CheckBox {
                text: "CPU";
                checked: true;
                width: g_width * 0.25px * 0.5;
                toggled => {
                    root.cpu_calc();
                    from_gpu.checked = !self.checked;
                }
            }

            from_gpu := CheckBox {
                text: "GPU";
                checked: false;
                width: g_width * 0.25px * 0.5;
                toggled => {
                    root.gpu_calc();
                    from_cpu.checked = !self.checked;
                }
            }
        }

        HorizontalLayout {
            Text {
                padding: 4px;
                vertical-alignment: center;
                text: "稀疏矩阵格式: ";
            }

            ComboBox {
                model: ["CSR", "COO"];
                current-value: "CSR";
                height: 35px;
                width: 120px;
                selected => {
                    root.select-matrix-format(self.current-value);
                }
            }
        }

        Button {
            //x:100px;
            //y:100px;
            text: "开始计算";
            height: 30px;
            clicked => {
                root.start-calc();
            }
        }
    }
}
